home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wil4c10.zip / HOST.C < prev    next >
C/C++ Source or Header  |  1997-07-26  |  8KB  |  315 lines

  1. /*
  2. **  --- host.c ---
  3. */
  4.  
  5. #include <windows.h>
  6. #include <winsock.h>
  7.  
  8. #include "wil.h"
  9. #include "message.h"
  10. #include "paint.h"
  11. #include "about.h"
  12.  
  13. #ifdef WIN32
  14. #define USE_INS HINSTANCE
  15. #define USE_PTR PSTR
  16. #else
  17. #define USE_INS HANDLE
  18. #define USE_PTR LPSTR
  19. #endif
  20.  
  21. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  22.  
  23. /* public globals */
  24.  
  25. HWND hMainWnd;            /* main window handle */
  26.  
  27. /* private globals */
  28.  
  29. #define BS         8
  30. #define LF        10
  31. #define CR        13
  32. #define MAX_BUF  128
  33.  
  34. #define GET_HOST      1
  35. #define GET_PROTO     2
  36. #define GET_SERVER    3
  37.  
  38. static HMENU hMenu;
  39. static USE_INS hInstance;
  40. static int WinWidth = 8 * NCOLS;
  41. static int WinHeight = 12 * NROWS + 48;
  42. static char Temp[MAX_BUF+8];
  43. static char InBufCmd = 0;
  44. static int  InBufLen = 0;
  45. static char InBuffer[MAX_BUF+1];
  46. static SOCKET Socket = 0;
  47. static HCURSOR ArrowCursor;
  48. static HCURSOR WaitCursor;
  49.  
  50. /* miscellaneous functions */
  51.  
  52. static void Add2Buffer(char Chr)
  53. {/* add char to input buffer */
  54.  switch(Chr)
  55.    {case BS:
  56.       if(InBufLen>0)
  57.         {/* backup on screen */
  58.          DisplayChar(BS);
  59.          /* remove last char from buffer */
  60.          InBufLen--;
  61.         }
  62.       break;
  63.     default:
  64.       /* display char */
  65.       DisplayChar(Chr);
  66.       /* put into buffer */
  67.       if(InBufLen<MAX_BUF) InBuffer[InBufLen++] = Chr;
  68.       break;
  69.    }
  70. }
  71.  
  72. static void DisplayError(int Code, LPSTR Msg)
  73. {DisplayString("ERROR: ");
  74.  if(Msg) DisplayString(Msg);
  75.  if(Code)
  76.    {wilErrorText(Code,(LPSTR)Temp,50);
  77.     DisplayLine((LPSTR)Temp);
  78.    }
  79. }
  80.  
  81. /* put cursor in window */
  82.  
  83. static void SetTheFocus(void)
  84. {/* create client area caret */
  85.  CreateCaret(hMainWnd,NULL,3,10);
  86.  SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  87.  ShowCaret(hMainWnd);
  88. }
  89.  
  90. /* WinMain */
  91.  
  92. #ifdef WIN32
  93. int WINAPI
  94. #else
  95. int PASCAL
  96. #endif
  97. WinMain(USE_INS hInst, USE_INS hPrevInstance,
  98.         USE_PTR szCmdLine,  int nCmdShow)
  99. {WNDCLASS  wc;
  100.  MSG msg;
  101.  BOOL Result;
  102.  if(!hPrevInstance)
  103.    {/* register main window class */
  104.     wc.style = CS_HREDRAW | CS_VREDRAW;
  105.     wc.lpfnWndProc = MainWndProc;
  106.     wc.cbClsExtra = 0;
  107.     wc.cbWndExtra = 0;
  108.     wc.hInstance = hInst;
  109.     wc.hIcon = LoadIcon(hInst, "HostIcon");
  110.     wc.hCursor = NULL;
  111.     wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  112.     wc.lpszMenuName =  "HostMenu";
  113.     wc.lpszClassName = "HostWClass";
  114.     Result = RegisterClass(&wc);
  115.     if(!Result) return FALSE;
  116.    }
  117.  
  118.  /* create main window */
  119.  hInstance = hInst;
  120.  hMainWnd = CreateWindow(
  121.         "HostWClass",   "HOST",    WS_OVERLAPPEDWINDOW,
  122.         CW_USEDEFAULT,  CW_USEDEFAULT,
  123.         WinWidth,       WinHeight,
  124.         NULL,           NULL,
  125.         hInstance,      NULL);
  126.  ShowWindow(hMainWnd, nCmdShow);
  127.  UpdateWindow(hMainWnd);
  128.  hMenu = GetMenu(hMainWnd);
  129.  
  130.  /* window control loop */
  131.  
  132.  while(GetMessage(&msg,NULL,0,0))
  133.    {
  134.     TranslateMessage(&msg);
  135.     DispatchMessage(&msg);
  136.    }
  137.  return (msg.wParam);
  138. } /* end WinMain */
  139.  
  140. #ifdef WIN32
  141. LRESULT CALLBACK
  142. #else
  143. long FAR PASCAL
  144. #endif
  145. MainWndProc(HWND hWindow,UINT iMsg,WPARAM wParam,LPARAM lParam)
  146. {int Code;
  147.  HDC hDC;
  148.  PAINTSTRUCT ps;
  149. #ifdef WIN32
  150. #else
  151.  static FARPROC lpfnAboutDlgProc;
  152. #endif
  153.  hMainWnd = hWindow;
  154.  switch (iMsg)
  155.     {case WM_CREATE:
  156.       /* create cursors */
  157.       ArrowCursor = LoadCursor(NULL, IDC_ARROW);
  158.       WaitCursor = LoadCursor(NULL, IDC_WAIT);
  159.       SetCursor(ArrowCursor);
  160. #ifdef WIN32
  161. #else
  162.       /* create thunk for Win16 */
  163.       lpfnAboutDlgProc = MakeProcInstance(AboutDlgProc,hInstance);
  164. #endif
  165.       /* initialize paint module */
  166.       PaintInit();
  167.       /* attach WINSOCK */
  168.       DisplayString("Attaching WINSOCK...");
  169.       Code = wilAttach();
  170.       DisplayLine("OK");
  171.       if(Code<0) DisplayError(Code,"wilAttach fails:");
  172.       else
  173.         {wsprintf((LPSTR)Temp," Description: %s", wilGetDescription() );
  174.          DisplayLine((LPSTR)Temp);
  175.          wsprintf((LPSTR)Temp," My HostName: %s", wilGetMyHostName() );
  176.          DisplayLine((LPSTR)Temp);
  177.          wsprintf((LPSTR)Temp," My HostAddr: %s", wilGetMyHostDotted(0) );
  178.          DisplayLine((LPSTR)Temp);
  179.         }
  180.       DestroyCaret();
  181.       break;
  182.  
  183.      case WM_COMMAND:
  184.          switch(wParam)
  185.            {
  186.             case MSG_ABOUT :
  187. #ifdef WIN32
  188.                DialogBox(hInstance, "AboutBox", hMainWnd, AboutDlgProc);
  189. #else
  190.                DialogBox(hInstance, "AboutBox", hMainWnd, lpfnAboutDlgProc);
  191. #endif
  192.                return 0;
  193.  
  194.             case MSG_BREAK:
  195.               wilCloseSocket(Socket);
  196.               break;
  197.  
  198.             case MSG_EXIT:
  199.               wilRelease();
  200.               DestroyWindow(hMainWnd);
  201.               break;
  202.  
  203.             case MSG_DEBUG:
  204.               Code = wilDebug(0);
  205.               wsprintf((LPSTR)Temp,"WIL library expires in %d seconds.",Code);
  206.               DisplayLine((LPSTR)Temp);
  207.               break;
  208.  
  209.             case MSG_GET_HOST:
  210.               InBufLen = 0;
  211.               InBufCmd = GET_HOST;
  212.               SetTheFocus();
  213.               DisplayString("Enter host name:");
  214.               break;
  215.  
  216.             case MSG_GET_PROTO:
  217.               InBufLen = 0;
  218.               InBufCmd = GET_PROTO;
  219.               SetTheFocus();
  220.               DisplayString("Enter protocol name:");
  221.               break;
  222.  
  223.             case MSG_GET_SERVE:
  224.               InBufLen = 0;
  225.               InBufCmd = GET_PROTO;
  226.               SetTheFocus();
  227.               DisplayString("Enter service name:");
  228.               break;
  229.            }
  230.          break;
  231.  
  232.     case WM_PAINT:
  233.       HideCaret(hMainWnd);
  234.       hDC = BeginPaint(hMainWnd, &ps);
  235.       SetMapMode(hDC,MM_ANISOTROPIC);
  236.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  237.       PaintMain(hDC,&ps);
  238.       EndPaint(hMainWnd,&ps);
  239.       SetCaretPos(PaintGetColPos(),PaintGetRowPos());
  240.       ShowCaret(hMainWnd);
  241.       break;
  242.  
  243.     case WM_DESTROY:
  244.       PostQuitMessage(0);
  245.       break;
  246.  
  247.     case WM_CHAR:
  248.       if(wParam==CR)
  249.         {/* do the CR */
  250.          DisplayChar((char)wParam);
  251.          /* user has completed input */
  252.          DisplayChar(LF);
  253.          InBuffer[InBufLen] = '\0';
  254.          DestroyCaret();
  255.          SetCursor(WaitCursor);
  256.          /* execute command */
  257.          switch(InBufCmd)
  258.             {
  259.              case GET_HOST:
  260.                if(wilIsDotted((LPSTR)InBuffer))
  261.                   Code = wilAskHostByAddr((LPSTR)InBuffer);
  262.                else
  263.                   Code = wilAskHostByName((LPSTR)InBuffer);
  264.                if(Code<0)
  265.                  {DisplayError(Code,"GetHost:");
  266.                   break;
  267.                  }
  268.                InBufCmd = 0;
  269.                wsprintf((LPSTR)Temp,"Host name: %s %s (%lx)",
  270.                  wilGetHostName(), wilGetHostDotted(0), wilGetHostAddr(0) );
  271.                DisplayLine((LPSTR)Temp);
  272.                break;
  273.  
  274.              case GET_PROTO:
  275.                Code = wilAskProtoByName((LPSTR)InBuffer);
  276.                if(Code<0)
  277.                  {DisplayError(Code,NULL);
  278.                   break;
  279.                  }
  280.                InBufCmd = 0;
  281.                wsprintf((LPSTR)Temp,"Protocol name: %s (%d)",
  282.                wilGetProtoName(), wilGetProtoNumber() );
  283.                DisplayLine((LPSTR)Temp);
  284.                break;
  285.  
  286.              case GET_SERVER:
  287.                InBufCmd = 0;
  288.                Code = wilAskServByName((LPSTR)InBuffer,(LPSTR)"tcp");
  289.                if(Code<0)
  290.                  {DisplayError(Code, NULL);
  291.                   break;
  292.                  }
  293.                wsprintf((LPSTR)Temp,"Server name: %s (%d)",
  294.                  wilGetServName(), wilGetServPort() );
  295.                DisplayLine((LPSTR)Temp);
  296.                break;
  297.  
  298.              default:
  299.                break;
  300.             } /* end-switch(InBufCmd) */
  301.          SetCursor(ArrowCursor);
  302.         }
  303.       else
  304.         {/* add char to input buffer */
  305.          Add2Buffer((char) wParam);
  306.         }
  307.       break;
  308.  
  309.     default:
  310.       return (DefWindowProc(hMainWnd, iMsg, wParam, lParam));
  311.    }
  312.  return 0;
  313.  
  314. } /* end MainWndProc */                                                                                          
  315.